Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spanify indexed reader classes #380

Merged
merged 2 commits into from
Jan 29, 2024
Merged

Spanify indexed reader classes #380

merged 2 commits into from
Jan 29, 2024

Conversation

drewnoakes
Copy link
Owner

Allows avoidance of temporary byte arrays, reduces the number of virtual calls to GetByteInternal, and uses conversion methods optimised by the framework (e.g. BinaryPrimitives).

@drewnoakes
Copy link
Owner Author

@iamcarbon please take a look if you have a chance.


GetBytes(index, bytes);

return encoding.GetString(bytes);
Copy link
Collaborator

@iamcarbon iamcarbon Jan 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we bump netframework to 462, we may consider adding a polyfill for Encoding.GetString. This would remain allocation free, and simplify ^.

For example:

#if NETFRAMEWORK || NETSTANDARD1_3
internal static class EncodingExtensions
{
    public unsafe static string GetString(this Encoding encoding, ReadOnlySpan<byte> bytes)
    {
        fixed (byte* pBytes = bytes)
        {
            return encoding.GetString(pBytes, bytes.Length);
        }
    }
}
#endif

Copy link
Owner Author

@drewnoakes drewnoakes Jan 27, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a great approach and we could use it in a few places.

I'll have a think about bumping the target further. Are there other APIs in 462 you know of that'd be good to use?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Array.Empty is the only other one I've missed so far. That would allow us to kill Util.Empty. It's possible that the lowering of [] may also be improved on net462.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm good with bumping to net462. 4.5.2 went out of support in 2022. 4.6.2 is supported until 2027. I'll file a different PR for that then rebase and update this one.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines +239 to +245
Span<byte> bytes = stackalloc byte[8];

GetBytes(index, bytes);

return IsMotorolaByteOrder
? BinaryPrimitives.ReadUInt64BigEndian(bytes)
: BinaryPrimitives.ReadUInt64LittleEndian(bytes);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Much nicer!

Comment on lines -55 to -62
public override byte[] GetBytes(int index, int count)
{
ValidateIndex(index, count);

var bytes = new byte[count];
Array.Copy(_buffer, index + _baseOffset, bytes, 0, count);
return bytes;
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to see the allocating functions killed.

@iamcarbon
Copy link
Collaborator

iamcarbon commented Jan 27, 2024

Looks great. Just one comment about a future simplification. Also, really great to see the API surface simplified with Spans.

Allows avoidance of temporary byte arrays, reduces the number of virtual calls to `GetByteInternal`, and uses conversion methods optimised by the framework (e.g. `BinaryPrimitives`).
@drewnoakes drewnoakes force-pushed the spanify-indexed-reader branch from ef0dc55 to 3143322 Compare January 29, 2024 03:46
@drewnoakes
Copy link
Owner Author

I'll investigate the encoding changes in a separate PR.

@drewnoakes drewnoakes merged commit 9086953 into main Jan 29, 2024
2 checks passed
@drewnoakes drewnoakes deleted the spanify-indexed-reader branch January 29, 2024 03:52
@drewnoakes
Copy link
Owner Author

@iamcarbon let's get 2.9.0 out soon. If there are any more public API changes you think we'd want, specifically related to performance in response to targeting newer frameworks, let me know.

I'm thinking of getting a prerelease package out today. We can also test the new infra for publishing to NuGet by creating releases here on GitHub.

drewnoakes added a commit that referenced this pull request Jan 29, 2024
Originally proposed by @iamcarbon in #380 (comment)

This requires allowing unsafe blocks for `net462` and `netstandard1.3`.

Co-authored-by: Jason Nelson <[email protected]>
@drewnoakes
Copy link
Owner Author

Pre-release package is available, per #389 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants